package com.umeox.calculator;

import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupMenu;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;

import java.util.Locale;


public class Calculator extends ThemeAppCompatActivity implements View.OnClickListener, PopupMenu.OnMenuItemClickListener, Logic.Listener {
    private View mBackspaceButton;
    private View mClearButton;
    private CalculatorDisplay mDisplay;
    private History mHistory;
    EventListener mListener = new EventListener();
    private Logic mLogic;
    private View mOverflowMenuButton;
    private ViewPager mPager;
    private Persist mPersist;

    public void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.main);
        this.mPager = findViewById(R.id.panelswitch);
        if (this.mPager != null) {
            this.mPager.setAdapter(new PageAdapter(this.mPager));
        } else {
            TypedArray buttons = getResources().obtainTypedArray(R.array.buttons);
            for (int i = 0; i < buttons.length(); i++) {
                setOnClickListener(null, buttons.getResourceId(i, 0));
            }
            buttons.recycle();
        }
        if (this.mClearButton == null) {
            this.mClearButton = findViewById(R.id.clear);
            this.mClearButton.setOnClickListener(this.mListener);
            this.mClearButton.setOnLongClickListener(this.mListener);
        }
        if (this.mBackspaceButton == null) {
            this.mBackspaceButton = findViewById(R.id.del);
            this.mBackspaceButton.setOnClickListener(this.mListener);
            this.mBackspaceButton.setOnLongClickListener(this.mListener);
        }
        this.mPersist = new Persist(this);
        this.mPersist.load();
        this.mHistory = this.mPersist.history;
        this.mDisplay = findViewById(R.id.display);
        this.mLogic = new Logic(this, this.mHistory, this.mDisplay);
        this.mLogic.setListener(this);
        this.mLogic.setDeleteMode(this.mPersist.getDeleteMode());
        this.mLogic.setLineLength(this.mDisplay.getMaxDigits());
        this.mHistory.setObserver(new HistoryAdapter(this, this.mHistory, this.mLogic));
        if (this.mPager != null) {
            this.mPager.setCurrentItem(state == null ? 0 : state.getInt("state-current-view", 0));
        }
        this.mListener.setHandler(this.mLogic, this.mPager);
        this.mDisplay.setOnKeyListener(this.mListener);
        if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
            createFakeMenu();
        }
        this.mLogic.resumeWithHistory();
        updateDeleteMode();
    }

    private void updateDeleteMode() {
        if (this.mLogic.getDeleteMode() == 0) {
            this.mClearButton.setVisibility(View.VISIBLE);
            this.mBackspaceButton.setVisibility(View.VISIBLE);
            return;
        }
        this.mClearButton.setVisibility(View.VISIBLE);
        this.mBackspaceButton.setVisibility(View.VISIBLE);
    }

    /* access modifiers changed from: package-private */
    public void setOnClickListener(View root, int id) {
        (root != null ? root.findViewById(id) : findViewById(id)).setOnClickListener(this.mListener);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean z;
        boolean z2 = false;
        super.onPrepareOptionsMenu(menu);
        MenuItem findItem = menu.findItem(R.id.basic);
        if (!getBasicVisibility()) {
            z = true;
        } else {
            z = false;
        }
        findItem.setVisible(z);
        MenuItem findItem2 = menu.findItem(R.id.advanced);
        if (!getAdvancedVisibility()) {
            z2 = true;
        }
        findItem2.setVisible(z2);
        return true;
    }

    private void createFakeMenu() {
        this.mOverflowMenuButton = findViewById(R.id.overflow_menu);
        if (this.mOverflowMenuButton != null) {
            this.mOverflowMenuButton.setVisibility(View.VISIBLE);
            this.mOverflowMenuButton.setOnClickListener(this);
        }
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.overflow_menu /*{ENCODED_INT: 2131492880}*/:
                PopupMenu menu = constructPopupMenu();
                if (menu != null) {
                    menu.show();
                    return;
                }
                return;
            default:
                return;
        }
    }

    private PopupMenu constructPopupMenu() {
        PopupMenu popupMenu = new PopupMenu(this, this.mOverflowMenuButton);
        this.mOverflowMenuButton.setOnTouchListener(popupMenu.getDragToOpenListener());
        Menu menu = popupMenu.getMenu();
        popupMenu.inflate(R.menu.menu);
        popupMenu.setOnMenuItemClickListener(this);
        onPrepareOptionsMenu(menu);
        return popupMenu;
    }

    public boolean onMenuItemClick(MenuItem item) {
        return onOptionsItemSelected(item);
    }

    private boolean getBasicVisibility() {
        return this.mPager != null && this.mPager.getCurrentItem() == 0;
    }

    private boolean getAdvancedVisibility() {
        return this.mPager != null && this.mPager.getCurrentItem() == 1;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.clear_history /*{ENCODED_INT: 2131492901}*/:
                this.mHistory.clear();
                this.mLogic.onClear();
                break;
            case R.id.advanced /*{ENCODED_INT: 2131492902}*/:
                if (!getAdvancedVisibility() && this.mPager != null) {
                    this.mPager.setCurrentItem(1, true);
                    break;
                }
            case R.id.basic /*{ENCODED_INT: 2131492903}*/:
                if (!getBasicVisibility() && this.mPager != null) {
                    this.mPager.setCurrentItem(0, true);
                    break;
                }
        }
        return super.onOptionsItemSelected(item);
    }

    /* access modifiers changed from: protected */
    public void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState(state);
        if (this.mPager != null) {
            state.putInt("state-current-view", this.mPager.getCurrentItem());
        }
    }

    public void onPause() {
        super.onPause();
        this.mLogic.updateHistory();
        this.mPersist.setDeleteMode(this.mLogic.getDeleteMode());
        this.mPersist.save();
    }

    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        if (keyCode != 4 || !getAdvancedVisibility() || this.mPager == null) {
            return super.onKeyDown(keyCode, keyEvent);
        }
        this.mPager.setCurrentItem(0);
        return true;
    }

    static void log(String message) {
    }

    @Override // com.umeox.calculator.Logic.Listener
    public void onDeleteModeChange() {
        updateDeleteMode();
    }

    class PageAdapter extends PagerAdapter {
        private View mAdvancedPage;
        private View mSimplePage;

        public PageAdapter(ViewPager parent) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View simplePage = inflater.inflate(R.layout.simple_pad, (ViewGroup) parent, false);
            View advancedPage = inflater.inflate(R.layout.advanced_pad, (ViewGroup) parent, false);
            this.mSimplePage = simplePage;
            this.mAdvancedPage = advancedPage;
            Resources res = Calculator.this.getResources();
            // 数字输入
            TypedArray digitButtons = res.obtainTypedArray(R.array.digit_buttons);
            for (int i = 0; i < digitButtons.length(); i++) {
                int id = digitButtons.getResourceId(i, 0);
                Button btn = simplePage.findViewById(id);
                String digit = String.format(Locale.getDefault(), "%d", i);
                btn.setText(digit);
                btn.setOnClickListener(mListener);
            }
            digitButtons.recycle();
            // 功能输入
            TypedArray simpleButtons = res.obtainTypedArray(R.array.simple_buttons);
            for (int i = 0; i < simpleButtons.length(); i++) {
                Calculator.this.setOnClickListener(simplePage, simpleButtons.getResourceId(i, 0));
            }
            simpleButtons.recycle();
//            TypedArray advancedButtons = res.obtainTypedArray(R.array.advanced_buttons);
//            for (int i2 = 0; i2 < advancedButtons.length(); i2++) {
//                Calculator.this.setOnClickListener(advancedPage, advancedButtons.getResourceId(i2, 0));
//            }
//            advancedButtons.recycle();
            View clearButton = simplePage.findViewById(R.id.clear);
            if (clearButton != null) {
                Calculator.this.mClearButton = clearButton;
            }
            View backspaceButton = simplePage.findViewById(R.id.del);
            if (backspaceButton != null) {
                Calculator.this.mBackspaceButton = backspaceButton;
            }
        }

        @Override // android.support.v4.view.PagerAdapter
        public int getCount() {
            return 1;
        }

        @Override // android.support.v4.view.PagerAdapter
        public void startUpdate(View container) {
        }

        @Override // android.support.v4.view.PagerAdapter
        public Object instantiateItem(View container, int position) {
            View page = this.mSimplePage;
            ((ViewGroup) container).addView(page);
            return page;
        }

        @Override // android.support.v4.view.PagerAdapter
        public void destroyItem(View container, int position, Object object) {
            ((ViewGroup) container).removeView((View) object);
        }

        @Override // android.support.v4.view.PagerAdapter
        public void finishUpdate(View container) {
        }

        @Override // android.support.v4.view.PagerAdapter
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override // android.support.v4.view.PagerAdapter
        public Parcelable saveState() {
            return null;
        }

        @Override // android.support.v4.view.PagerAdapter
        public void restoreState(Parcelable state, ClassLoader loader) {
        }
    }
}
